using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace TTDotNet.Engine.GUI
{
    public class ListableItem : Control
    {
        private bool _selected;

        private List<Control> parts;

        public ListableItem()
        {
            parts = new List<Control>();
        }

        public void AddControl(Control c)
        {
            parts.Add(c);
        }

        public override void Render(PointF location)
        {
            foreach (Control c in parts)
            {
                c.Render(PointF.Add(location, new SizeF(_size.X, _size.Y)));
            }
        }

        public Control GetControl(int num)
        {
            return parts[num];
        }

        public override bool MouseHandler(Point cursor, bool[] buttons, float zDelta)
        {
            
            if (Contains(cursor))
            {
                

                if (!buttons[0])
                {
                    if (_mouseDown)
                    {
                        _mouseDown = false;
                        OnMouseRelease(cursor);
                        
                        DoControl();
                    }
                }
                else if (buttons[0])
                {
                    _mouseDown = true;
                    OnMouseDown(cursor, buttons);
                }
                return true;
            }
            return false;
        }

        protected override void OnMouseDown(Point cursor, bool[] buttons)
        {
            foreach (Control c in parts)
            {
                c.MouseHandler(Point.Subtract(cursor, new Size((int)_size.X, (int)_size.Y)), buttons, 0);
            }
        }

        protected override void OnMouseRelease(Point cursor)
        {
            foreach (Control c in parts)
            {
                c.MouseHandler(Point.Subtract(cursor, new Size((int)_size.X, (int)_size.Y)), new bool[3], 0);
            }
        }

        public bool Selected
        {
            get { return _selected; }
            set { _selected = value; }
        }

        public override void SetSize(float newWidth, float newHeight)
        {
            _size.Width = newWidth;
            _size.Height = newHeight;
        }

        public override PointF Position
        {
            get
            {
                return _size.Location;
            }
            set
            {
                _size.Location = value;
            }
        }

    }
}
